package com.group6.exam.manager.service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSON;
import com.group6.exam.common.entity.Menu;
import com.group6.exam.common.entity.PageModel;
import com.group6.exam.common.util.CollectionUtil;
import com.group6.exam.manager.dao.IRoleDao;
import com.group6.exam.manager.dao.RoleDaoImpl;

public class RoleServiceImpl implements IRoleService {

	private IRoleDao roleDao = new RoleDaoImpl();

	@Override
	public List<Map<String, Object>> getAllRole() {
		return roleDao.getAllRole();
	}

	/**
	 * 查看角色信息
	 */
	public PageModel check(PageModel pageModel) {
		String roleId = pageModel.getRoleId();
		// 查询总条数
		int totalRecords = roleDao.getTotalRecords(roleId);
		int pageSize = pageModel.getPageSize();
		// 获取总页数
		PageModel page = new PageModel(totalRecords, pageSize);
		// 设置当前页
		page.setCurrentPage(pageModel.getCurrentPage());
		List<Map<String, Object>> mapList = new ArrayList<>();
		List<Map<String, Object>> List = roleDao.check(pageModel);
		for (Map<String, Object> map : List) {
			String role_id = map.get("role_id") + "";
			String rolename = roleDao.getRoleName(role_id);
			map.put("role_name", rolename);
			mapList.add(map);
		}
		page.setList(mapList);
		return page;
	}

	@Override
	public int deleteRole(String roleId) {
		return roleDao.deleteRole(roleId);
	}

	@Override
	public int addRole(String roleName) {
		return roleDao.addRole(roleName);
	}

	@Override
	public Map<String, Object> initRole(String roleId) {
		return roleDao.initRole(roleId);
	}

	@Override
	public int update(String roleName, String roleId) {
		return roleDao.update(roleName, roleId);
	}

	/**
	 * 查询系统中所有权限json字符串
	 */
	@Override
	public String getRights() {
		// 创建一个存储权限菜单的集合对象
		List<Menu> menus = new ArrayList<Menu>();

		List<Map<String, Object>> list = roleDao.getRights();
		// System.out.println(JSON.toJSONString(list,true).replaceAll("rightId",
		// "id").replaceAll("parentId", "pId").replaceAll("rightName", "name"));
		if (CollectionUtil.isEmpty(list)) {
			for (Map<String, Object> map : list) {
				String mapJsonString = JSON.toJSONString(map);
				mapJsonString = mapJsonString.replace("right_id", "id")
						.replace("parent_id", "pId")
						.replace("right_name", "name");

				// 把处理好的json字符串对象转换为Menu对象
				Menu menu = JSON.parseObject(mapJsonString, Menu.class);

				// 把转换好的对象存储到Menu集合对象中
				menus.add(menu);
			}
		}

		return JSON.toJSONString(menus, true).replaceAll("pid", "pId");
	}

	@Override
	public String getMeunsByRoleId(String roleId) {
		// 创建一个存储权限菜单的集合对象
		List<Menu> menus = new ArrayList<Menu>();

		List<Map<String, Object>> list = roleDao.getMeunsByRoleId(roleId);
		// System.out.println(JSON.toJSONString(list,true).replaceAll("rightId",
		// "id").replaceAll("parentId", "pId").replaceAll("rightName", "name"));
		if (CollectionUtil.isEmpty(list)) {
			for (Map<String, Object> map : list) {
				String mapJsonString = JSON.toJSONString(map);
				mapJsonString = mapJsonString.replace("right_id", "id")
						.replace("parent_id", "pId")
						.replace("right_name", "name");

				// 把处理好的json字符串对象转换为Menu对象
				Menu menu = JSON.parseObject(mapJsonString, Menu.class);

				// 把转换好的对象存储到Menu集合对象中
				menus.add(menu);
			}
		}

		return JSON.toJSONString(menus, true).replaceAll("pid", "pId");
	}

	@Override
	public boolean grant(String rightIds, String roleId) {
		boolean flag = false;
		String[] rightIdss = rightIds.split(",");

		// 判断当前角色是否已经授权
		if (!roleDao.isGrant(roleId)) {
			if (rightIdss != null && rightIdss.length > 0) {
				for (String rightId : rightIdss) {
					// 重新授予权限
					int row = roleDao.permission(rightId, roleId);
					flag = row > 0 ? true : false;
				}
			}
		} else {
			// 如果已经授权废弃原来的所有权限
			if (roleDao.revoke(roleId) > 0) {
				if (rightIdss != null && rightIdss.length > 0) {
					for (String rightId : rightIdss) {
						// 重新授予权限
						int row = roleDao.permission(rightId, roleId);
						flag = row > 0 ? true : false;
					}
				}
			}
		}

		return flag;
	}

}
